﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BEPUphysics.Collidables;
using BEPUphysics.DataStructures;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using BEPUphysics.MathExtensions;
using Brain;
using BEPUphysics;

namespace Brain.Bepu
{
    public class PhysicObject_TriangleMesh : IPhysicsObject3D
    {
        public Vector3 RotationRadians
        {
            get { return Vector3.Zero; }
            set { Orientation = QuaternionUtil.CreateQuaternion(value); }
        }
        public void Dispose()
        {
            this.Space.Remove(this.Mesh);
        }
        public event SetVoid OnUpdate;
        public StaticMesh Mesh;

        /// <summary>
        /// NOT IMPLEMENTED
        /// </summary>
        public Vector3 LinearVelocity
        {
            get;
            set;
        }

        /// <summary>
        /// NOT IMPLEMENTED
        /// </summary>
        public Quaternion Orientation
        {
            get;
            set;
        }
        /// <summary>
        /// NOT IMPLEMENTED
        /// </summary>
        public Vector3 AngularVelocity
        {
            get;
            set;
        }

        private string modelName;
        public string ModelName
        {
            get { return modelName; }
            set
            {
                modelName = value;
                Model model = Engine.Instance.ContentManager.Load<Model>(value);
                Vector3[] staticTriangleVertices;
                int[] staticTriangleIndices;

                TriangleMesh.GetVerticesAndIndicesFromModel(model,
                    out staticTriangleVertices, out staticTriangleIndices);
                if (Mesh != null)
                {
                    Space.Remove(Mesh);
                }
                Mesh = new StaticMesh(staticTriangleVertices, staticTriangleIndices,
                    new AffineTransform(Vector3.Zero));
                Mesh.Sidedness = BEPUphysics.CollisionShapes.ConvexShapes.TriangleSidedness.Counterclockwise;

                Space.Add(Mesh);
            }
        }

        public Space Space { get; set; }

        public PhysicObject_TriangleMesh(Space space)
        {
            this.Space = space;
        }
        public PhysicObject_TriangleMesh(string ModelName, Space space)
        {
            this.Space = space;
            this.ModelName = ModelName;
            this.Mesh.Tag = this;
        }

        public object Tag { get; set; }

        public Vector3 Position { get; set; }

        public Vector3 Scale { get; set; }

        public bool Immovable { get; set; }

        public float Mass { get; set; }

        public bool ShouldSerialize { get; set; }

        public void SerializeData(SerializationData parentData, string index)
        {
            // We do NOT need to serialize Position/Scale:
            // Position : The WorldObject parent of this PhysicsObject will save the Position
            // Scale    : The WorldObject has an Offset for setting this Scale

            //parentData.AddData("PO.Rotation", this.Orientation, index);
            //parentData.AddData("PO.Immovable", this.Immovable, index);
        }

        public void DeserializeData(SerializationData parentData, string index)
        {
            //this.Orientation = parentData.GetData<Quaternion>("PO.Rotation", index);
            //this.Immovable = parentData.GetData<bool>("PO.Immovable", index);
        }

        public void DeserializedAll()
        {
        }
    }
}
